/**
 * 权限
 * WangPeixi
 */
var id = "";
var addForm = "";
$(function() {
	id = request.QueryString("id");// 从跳转页面过来的
	addForm = $("#addForm");
	initCombobox();
	initTree();
	addForm.form("disableValidation");
	$("#btncancel").click(function() {
		addForm.form('reset');
		addForm.form("disableValidation");
	});
	addForm.form("disableValidation");
});
function initCombobox() {
	// 初始化状态下拉菜单
	$('#type').combobox({
		editable : false, // 不可编辑状态
		cache : false,
		panelHeight : 'auto',// 自动高度适合
		valueField : 'code',
		textField : 'name'
	});
	// 数据放入下拉菜单
	var ut = postAsyncAjax("/findCodebySortCode?sortCode=006", null);// 获取状态
	if (ut.data == null)
		ut.data = [];
	ut.data.unshift({
		"code" : "",
		"name" : "--请选择--"
	});
	$("#type").combobox("clear");
	$("#type").combobox("loadData", ut.data);
}

/**
 * 刷新grid
 */
function reloadGird() {
	gridReload("limitsList");
}

function addBankAccount() {
	var parentCode = $('#parentCode').datebox("getValue");
	
	if (addForm.form('enableValidation')) {
		addForm.form('submit', {
			url : '/addLimits?parentCode=' + parentCode,
			success : function(msg) {
				msg = $.parseJSON(msg);
				if (msg.returnCode == '00') {
					top.showMessage("添加成功,请手动刷新！");
					closeIframeWindow();
					reloadGird();
				} else {
					top.showMessage("添加失败!");
				}
			}
		});
	}
}
function initTree() {
	var state ="";
	var t = 1;
	var param = [];
	var limit = postAsyncAjax("/findLimitsByParentCode?parentCode=-1").data;
	for (var i = 0; i < limit.length; i++) {
		param.push({
			"id" : limit[i].code,
			"text" : limit[i].name,
			"state" : "closed",
			"iconCls" : "icon-group",
			"t":t
		})
	}
	
	$("#parentCode").combotree({
		data : param,
		checkbox : false,
		cascadeCheck : false,
		required: true,  
		lines : true,
		onBeforeExpand : function(note) {
			if (note.t == 1 || !note.state) {
				userTreeLoad(note);
			}
		}
	});
	
	function userTreeLoad(note) {
		var param = new Array();
		var limit = postAsyncAjax("/findLimitsByParentCode?parentCode="+ note.id).data;
		for (var i = 0; i < limit.length; i++) {
			param.push({
				"id" : limit[i].code,
				"text" : limit[i].name,
				"state" : "closed",
				"t":t
			})
		}
		$('#parentCode').combotree('tree').tree('append',{
			parent : note.target,
			data : param
		});
		note.t = 2;
		
	}
	
	
}
